package com.sdgakj.utils;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sdgakj.enums.EnumJpushTag;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushUtil {

	private static Logger logger = LoggerFactory.getLogger(JPushUtil.class);
	private static final String APPKEY_STUDENT = "15d23c61b7a97d3684333575";
	private static final String MASTERSECRET_STUDENT = "2019a587511a2bb9367546ec";
	
	private static final String APPKEY_STUDENT1 = "5e6fff2ebb1c74c69f021c9a";
	private static final String MASTERSECRET_STUDENT1 = "8992238c25cd74a61932d111";
	
	private static JPushClient jpushClientStudent = new JPushClient(MASTERSECRET_STUDENT, APPKEY_STUDENT, 3);
	
	private static JPushClient jpushClientCheck = new JPushClient(MASTERSECRET_STUDENT1, APPKEY_STUDENT1, 3);

	/**
	 * 通过别名推送
	 * @param msg
	 * @param type
	 */
	public static void pushByAlias(String alias, String msg,String type) {
		List<PushPayload> payloadList = buildPushObject_all_alias_alert(alias,msg,type);
			if(payloadList!=null && payloadList.size()>0){
				for(PushPayload payload:payloadList){
					try {						
						PushResult result = jpushClientStudent.sendPush(payload);
						logger.info("Got result - " + result);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
	}
	
	/**
	 * 同时发送ios和安卓
	 * @param msg
	 * @param type
	 */
	public static void pushAllByAlias(String alias, String msg,String type) {
//		List<PushPayload> payloadList = buildPushObject_all_alias_alert(alias,msg,type);
//			if(payloadList!=null && payloadList.size()>0){
//				for(PushPayload payload:payloadList){
//					try {						
//						PushResult result = jpushClientStudent.sendPush(payload);
//						logger.info("Got result - " + result);
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}
			PushPayload payload =PushPayload.newBuilder()
	                .setPlatform(Platform.all())
	                .setAudience(Audience.alias(alias))  
	                .setNotification(Notification.newBuilder()
	                		.setAlert(msg)
	                		.addPlatformNotification(AndroidNotification.newBuilder()
	                				.setTitle(msg).build())
	                		.addPlatformNotification(IosNotification.newBuilder()
	                				.setAlert(msg)
    								.setBadge(5)
    								.setSound("happy")
	                				.setBadge(5)
	                				.build())
	                		.build())
	                .build();
			try {
				PushResult result = jpushClientStudent.sendPush(payload);
				logger.info("Got result - " + result);
			} catch (APIConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (APIRequestException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public static void pushAllByAliasByPuchase(String alias, String msg,String type,Long purchaseId, String name) {
//		List<PushPayload> payloadList = buildPushObject_all_alias_alert(alias,msg,type);
//			if(payloadList!=null && payloadList.size()>0){
//				for(PushPayload payload:payloadList){
//					try {						
//						PushResult result = jpushClientStudent.sendPush(payload);
//						logger.info("Got result - " + result);
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}
			PushPayload payload =PushPayload.newBuilder()
	                .setPlatform(Platform.all())
	                .setAudience(Audience.alias(alias))  
	                .setNotification(Notification.newBuilder()
	                		.setAlert(msg)
	                		.addPlatformNotification(AndroidNotification.newBuilder()
	                				.addExtra("id",purchaseId.toString())
	                				.addExtra("type",name)
	                				.setTitle(msg).build())	                				
	                		.addPlatformNotification(IosNotification.newBuilder()
	                				.addExtra("id", purchaseId.toString())
	                				.addExtra("type",name)
	                				.setAlert(msg)
    								.setBadge(5)
    								.setSound("happy")
	                				.setBadge(5)
	                				.build())
	                		.build())
							.setOptions(Options.newBuilder()
							        .setApnsProduction(PropertiesUtil.getApnsProduction())
							        .build())
							//.setMessage(Message.newBuilder()
							//        .setMsgContent(msgContent)
							//        .addExtra("id", msgId.toString())
							//        .addExtra("relId", msgRelId.toString())
							//        .addExtra("type", msgType.toString())
							//        .addExtra("content", msgContent)
							//        .build())
							.build();
			try {
				PushResult result = jpushClientStudent.sendPush(payload);
				logger.info("Got result - " + result);
			} catch (APIConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (APIRequestException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

    public static void pushAllByAliasByPuchase(String alias, String msg,String type,String purchaseId, String name) {
//		List<PushPayload> payloadList = buildPushObject_all_alias_alert(alias,msg,type);
//			if(payloadList!=null && payloadList.size()>0){
//				for(PushPayload payload:payloadList){
//					try {
//						PushResult result = jpushClientStudent.sendPush(payload);
//						logger.info("Got result - " + result);
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}
        PushPayload payload =PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(msg)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtra("id",purchaseId.toString())
                                .addExtra("type",name)
//                                .addExtra("title",purchaseId)
                                .setTitle(msg).build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("id", purchaseId)
                                .addExtra("type",name)
                                .setAlert(msg)
                                .setBadge(1)
                                .setSound("happy")
                                .setBadge(1)
                                .build())

                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(PropertiesUtil.getApnsProduction())
                        .build())
                //.setMessage(Message.newBuilder()
                //        .setMsgContent(msgContent)
                //        .addExtra("id", msgId.toString())
                //        .addExtra("relId", msgRelId.toString())
                //        .addExtra("type", msgType.toString())
                //        .addExtra("content", msgContent)
                //        .build())
                .build();
        try {
            PushResult result = jpushClientStudent.sendPush(payload);
            logger.info("Got result - " + result);
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
	
	/**
	 * 通过别名推送
	 * @param msg
	 * @param type
	 */
	public static void pushByAliasCheck(String alias, String msg,String type) {
		List<PushPayload> payloadList = buildPushObject_all_alias_alert(alias,msg,type);

			if(payloadList!=null && payloadList.size()>0){
				for(PushPayload payload:payloadList){
					try {
						PushResult result = jpushClientCheck.sendPush(payload);
						logger.info("Got result - " + result);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
	}
	
	/**
	 * 通过Tag推送
	 */
	public static void pushByTag(String tag, String msg,String type) {
		List<PushPayload> payloadList =  buildPushObject_all_tag_alert(tag, msg,type);

			if(payloadList!=null && payloadList.size()>0){
				for(PushPayload payload:payloadList){
					try{						
						PushResult result = jpushClientStudent.sendPush(payload);
						logger.info("Got result - " + result);
					}catch(Exception e){
						e.printStackTrace();
					}
				}
			}
		
	}
	
	/**
	 * 全部推送
	 * @param msg
	 * @param type
	 */
	public static void pushByAll(String msg,String type) {
		List<PushPayload> payloadList = buildPushObject_all(msg,type);

			if(payloadList!=null && payloadList.size()>0){
				for(PushPayload payload:payloadList){
					try {
						PushResult result = jpushClientStudent.sendPush(payload);
						logger.info("Got result - " + result);						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
	
	}
	
	
	/**
	 * 根据alias推送
	 * @param alias  别名
	 * @param title      推送内容
	 * @return
	 */
	private static List<PushPayload> buildPushObject_all_alias_alert(String alias, String msg,String type) {
		List<PushPayload> list = new ArrayList<PushPayload>();
		Platform platform = Platform.android();
	 if(EnumJpushTag.IOS.getCode().equals(type)){
			platform = Platform.ios();
			list.add(PushPayload.newBuilder()
            .setPlatform(platform)
            .setAudience(Audience.alias(alias))
            .setNotification(Notification.newBuilder()
                    .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(msg)
                            .setBadge(5)
                            .setSound("happy.caf")
                            .build())
                    .build())
            .setOptions(Options.newBuilder()
            .setApnsProduction(PropertiesUtil.getApnsProduction())
            .build()).build());
		}else if(EnumJpushTag.ANDROID.getCode().equals(type)){
			platform = Platform.android();
			list.add(PushPayload.newBuilder()
            .setPlatform(platform)
            .setAudience(Audience.alias(alias))
            .setNotification(Notification.alert(msg))
            .setOptions(Options.newBuilder()
            .setApnsProduction(PropertiesUtil.getApnsProduction())
            .build()).build());
		}
		
		else{
//			list.add(PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.alias(alias)).setNotification(Notification.alert(msg))
//			        .setOptions(Options.newBuilder().setApnsProduction(PropertiesUtil.getApnsProduction()).build()).build());
//					list.add(PushPayload.newBuilder()
//				            .setPlatform(Platform.ios())
//				            .setAudience(Audience.alias(alias))
//				            .setNotification(Notification.newBuilder()
//				                    .addPlatformNotification(IosNotification.newBuilder()
//				                            .setAlert(msg)
//				                            .setBadge(5)
//				                            .setSound("happy.caf")
//				                            .build())
//				                    .build())
//				            .setOptions(Options.newBuilder()
//				            .setApnsProduction(PropertiesUtil.getApnsProduction())
//				            .build()).build());
			list.add(PushPayload.newBuilder()
	                .setPlatform(Platform.all())
	                .setAudience(Audience.all())
	                .setNotification(Notification.alert(msg))
	                .setOptions(Options.newBuilder()
	                        .setApnsProduction(PropertiesUtil.getApnsProduction())
	                        .build())
	                .build());
		}
		return list;
	}
	
	/**
	 * 根据tag推送
	 * @param alias  别名
	 * @param title      推送内容
	 * @return
	 */
	private static List<PushPayload> buildPushObject_all_tag_alert(String tag, String msg,String type) {
		List<PushPayload> list = new ArrayList<PushPayload>();
		Platform platform = Platform.android();
	    if(EnumJpushTag.IOS.getCode().equals(type)){
			platform = Platform.ios();
			list.add( PushPayload.newBuilder()
	                .setPlatform(platform)
	                .setAudience(Audience.tag(tag))
	                .setNotification(Notification.newBuilder()
	                        .addPlatformNotification(IosNotification.newBuilder().setAlert(msg).setBadge(5).setSound("happy.caf").build())
	                        .build())
	                .setOptions(Options.newBuilder()
	                .setApnsProduction(PropertiesUtil.getApnsProduction())
	                .build()).build());
		}else if(EnumJpushTag.ANDROID.getCode().equals(type)){
			platform = Platform.android();
			list.add(PushPayload.newBuilder()
	                .setPlatform(platform)
	                .setAudience(Audience.tag(tag))
	                .setNotification(Notification.alert(msg))
	                .setOptions(Options.newBuilder()
	                .setApnsProduction(PropertiesUtil.getApnsProduction())
	                .build()).build());
		}
		else{
//			list.add(PushPayload.newBuilder()
//	        .setPlatform(Platform.android())
//	        .setAudience(Audience.tag(tag))
//	        .setNotification(Notification.alert(msg))
//	        .setOptions(Options.newBuilder()
//	        .setApnsProduction(PropertiesUtil.getApnsProduction())
//	        .build()).build());
//			
//			 list.add(PushPayload.newBuilder()
//		                .setPlatform(Platform.ios())
//		                .setAudience(Audience.tag(tag))
//		                .setNotification(Notification.newBuilder()
//		                        .addPlatformNotification(IosNotification.newBuilder()
//		                                .setAlert(msg)
//		                                .setBadge(5)
//		                                .setSound("happy.caf")
//		                                .build())
//		                        .build())
//		                .setOptions(Options.newBuilder()
//		                .setApnsProduction(PropertiesUtil.getApnsProduction())
//		                .build()).build());
			list.add(PushPayload.newBuilder()
	                .setPlatform(Platform.all())
	                .setAudience(Audience.all())
	                .setNotification(Notification.alert(msg))
	                .setOptions(Options.newBuilder()
	                        .setApnsProduction(PropertiesUtil.getApnsProduction())
	                        .build())
	                .build());
		}
		return list;
	}
	
	
	/**
	 * 全部推送
	 * @param msg 推送内容
	 * @return
	 */
	
	private static List<PushPayload> buildPushObject_all(String msg,String type) {
		List<PushPayload> list = new ArrayList<PushPayload>();
		Platform platform = Platform.android();
	    if(EnumJpushTag.IOS.getCode().equals(type)){
			platform = Platform.ios();
			list.add(PushPayload.newBuilder()
	                .setPlatform(platform)
	                .setAudience(Audience.all())
	                .setNotification(Notification.newBuilder()
	                        .addPlatformNotification(IosNotification.newBuilder()
	                                .setAlert(msg)
	                                .setBadge(5)
	                                .setSound("happy.caf")
	                                .build())
	                        .build())
	                .setOptions(Options.newBuilder()
	                .setApnsProduction(PropertiesUtil.getApnsProduction())
	                .build()).build());
		}else if(EnumJpushTag.ANDROID.getCode().equals(type)){
			platform = Platform.android();
			list.add(PushPayload.newBuilder()
	                .setPlatform(platform)
	                .setAudience(Audience.all())
	                .setNotification(Notification.alert(msg))
	                .setOptions(Options.newBuilder()
	                .setApnsProduction(PropertiesUtil.getApnsProduction())
	                .build()).build());
		}
		else{
//		   list.add(
//			PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all()).setNotification(Notification.alert(msg)).setOptions(Options.newBuilder()
//            .setApnsProduction(PropertiesUtil.getApnsProduction()).build()).build());
//		
//		   list.add(PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
//                .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(IosNotification.newBuilder()
//                                .setAlert(msg)
//                                .setBadge(5)
//                                .setSound("happy.caf")
//                                .build())
//                        .build())
//                .setOptions(Options.newBuilder()
//                .setApnsProduction(PropertiesUtil.getApnsProduction())
//                .build()).build());
			list.add(
					PushPayload.newBuilder()
	                .setPlatform(Platform.all())
	                .setAudience(Audience.all())
	                .setNotification(Notification.alert(msg))
	                .setOptions(Options.newBuilder()
	                        .setApnsProduction(PropertiesUtil.getApnsProduction())
	                        .build())
	                .build()
			);
			
	     }
		return list;
	}
	// 推送全部用户
	public static void pushAllUser(String msg) {
		PushPayload payload =buildPushObject_all_alias_alert(msg);
		try{
			PushResult result = jpushClientStudent.sendPush(payload);
			logger.info("Got result - " + result);				
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	// 推送给全部设备
	  public static PushPayload buildPushObject_all_alias_alert(String msg) {  
		  //为了android推送时候区分生产线和测试线
		  Audience audience = Audience.all();
		  if(!PropertiesUtil.getApnsProduction()) {
			 audience = Audience.tag_and("dev");
		  }
	        return PushPayload.newBuilder()  
	                .setPlatform(Platform.all())//设置接受的平台  
//	                .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到  
	                .setAudience(audience)
	                .setNotification(Notification.alert(msg))  
	                .setOptions(Options.newBuilder()
	                        .setApnsProduction(PropertiesUtil.getApnsProduction())
	                        .build())
	                .build();  
	    }  
	  
	  //推送给全部设备
	  public static void  buildPushObject_all_alias_alert_andriod(String msg) {  
		  //为了android推送时候区分生产线和测试线
		  Audience audience = Audience.all();
		  if(!PropertiesUtil.getApnsProduction()) {
			 audience = Audience.tag_and("dev");
		  }
		  PushPayload pushPayload = PushPayload.newBuilder()  
	                .setPlatform(Platform.android())//设置接受的平台  
//	                .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到  
	                .setAudience(audience)
	                .setNotification(Notification.alert(msg))
	                .setOptions(Options.newBuilder()
	                        .setApnsProduction(PropertiesUtil.getApnsProduction())
	                        .build())
	                .build();  
		  try{						
				PushResult result = jpushClientStudent.sendPush(pushPayload);
				logger.info("Got result - " + result);
			}catch(Exception e){
				e.printStackTrace();
			}
	  } 
	  
	  //推送给全部设备
	  public static void  buildPushObject_all_alias_alert_ios(String msg) {
		  	//为了android的dev设备推送冗余
			Audience audience = Audience.all();
			if(!PropertiesUtil.getApnsProduction()) {
				audience = Audience.tag_and("dev");
			}
		    PushPayload pushPayload = PushPayload.newBuilder()  
	                .setPlatform(Platform.ios())//设置接受的平台  
//	                .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到
	                .setAudience(audience)
	                .setNotification(Notification.alert(msg))
	                .setOptions(Options.newBuilder()
	                        .setApnsProduction(PropertiesUtil.getApnsProduction())
	                        .build())
	                .build();  
	        try{						
				PushResult result = jpushClientStudent.sendPush(pushPayload);
				logger.info("Got result - " + result);
			}catch(Exception e){
				e.printStackTrace();
			}
	  } 
	  
	  public static void buildPushObject_android_tag_alertWithTitle(String tag,String msg) {
		  PushPayload pushPayload = null;
		  //为了android推送时候区分生产线和测试线
		  if(!PropertiesUtil.getApnsProduction()) {
			  pushPayload = PushPayload.newBuilder()
		                .setPlatform(Platform.all())
//		                .setAudience(Audience.tag(tag, "dev"))
		                .setAudience(Audience.tag_and(tag, "dev"))
		                .setNotification(Notification.alert(msg))
		                .setOptions(Options.newBuilder()
		                        .setApnsProduction(PropertiesUtil.getApnsProduction())
		                        .build())
		                .build();
		  } else {
			  pushPayload = PushPayload.newBuilder()
		                .setPlatform(Platform.all())
		                .setAudience(Audience.tag(tag))
		                .setNotification(Notification.alert(msg))
		                .setOptions(Options.newBuilder()
		                        .setApnsProduction(PropertiesUtil.getApnsProduction())
		                        .build())
		                .build();
		  }
//		  PushPayload pushPayload =  PushPayload.newBuilder()
//	                .setPlatform(Platform.all())
//	                .setAudience(Audience.tag(tag))
//	                .setNotification(Notification.alert(msg))
//	                .setOptions(Options.newBuilder()
//	                        .setApnsProduction(PropertiesUtil.getApnsProduction())
//	                        .build())
//	                .build();  
	        try{						
				PushResult result = jpushClientStudent.sendPush(pushPayload);
				logger.info("Got result - " + result);
			}catch(Exception e){
				e.printStackTrace();
			}
	  }
}